<?php
/**
 * 标签名称，比如这个使用!{area }这样调用
 * @var string
 */
$self_tag_name = 'area';

/**
 * 标签方法入口，必须与文件名一致
 * @param array $params 参数
 * @param Smarty $smarty
 */
function smarty_function_area($params, &$smarty){
	if(isset($params['page'])){
		$page = trim($params['page']);
	}else{
		$page = 'index';
	}
	
	$area_id = trim($params['id']);
	
	//加载页面配置文件
	global $widget_page_config;
	if($widget_page_config == null){
		$widget_page_config = array();
		smarty_load_area_config($page, $widget_page_config, array(), true);
	}
	
	if(isset($widget_page_config['areas'][$area_id])){
		return smarty_load_area_widgets($widget_page_config['areas'][$area_id]);
	}
	return '';
}

/**
 * 加载配置文件
 * @param $name 要加载的配置文件
 * @param $page_config 配置：array()
 * @param $loaded_config 已经加载过的配置文件名称，用于判断是否已经加载过
 * @param $root 是否是根
 */
function smarty_load_area_config($name, &$page_config, $loaded_config, $root = false){
	if(in_array($name, $loaded_config)){
		//如果已经引入过，则直接退出
		return;
	}
	$loaded_config[] = $name;
	
	if($page_config == null)
		$page_config = array();
	$page_config_path = ROOT_PATH . '/sites/main/' . MAIN_DOMAIN . '/templates/data/page_config/' . $name . '.php';
	if(is_file($page_config_path)){
		$page_config_temp = require_once $page_config_path;
		if($page_config_temp == null){
			return;
		}
		if(isset($page_config_temp['include']) && is_array($page_config_temp['include'])){
			//如果存在include项，则引入其它配置
			foreach($page_config_temp['include'] as $include_config_name){
				smarty_load_area_config($include_config_name, $page_config, $loaded_config);
			}
			unset($page_config_temp['include']);
			reset($page_config_temp);
		}
		
		if($page_config_temp != null && is_array($page_config_temp)){
			$page_config = array_merge($page_config, $page_config_temp);
		}
	}
}

/**
 * 加载区域内的组件
 * @param $page_widget_configs
 */
function smarty_load_area_widgets($page_widget_configs){
	$html = '';
	if(isset($page_widget_configs) && is_array($page_widget_configs)){
		foreach ($page_widget_configs as $widget_config){
			$html .= smarty_get_widget_content($widget_config);
		}
	}
	return $html;
}

function smarty_get_widget_content($widget_config){
	global $loaded_widget_list;
	$widget_name = trim($widget_config['name']);
	if($loaded_widget_list == null){
		$loaded_widget_list = array();
	}else{
		if(in_array($widget_name, $loaded_widget_list)){
			throw new SmartyException('Loop loaded widget');
		}
	}
	
	$loaded_widget_list[] = $widget_name;
	
	$widget_template = trim($widget_config['template']);
	$widget_type = trim($widget_config['type']);
	
	$smarty = Viewer::get_view();
	$widget_class_name = ucfirst($widget_name) . 'Widget';
	//引入Widget文件
	$widget_path = ROOT_PATH . '/protected/includes/widgets/' . $widget_type . '/' . $widget_name;
	require_once $widget_path . '/' . $widget_class_name . '.php';
	$widget_obj = new $widget_class_name;
	$widget_obj->init(isset($widget_config['options']) ? $widget_config['options'] : null);
	$widget_data = $widget_obj->get_data();
	if($widget_data == null){
		$widget_data = array();
	}
	$widget_data['widget_config'] = $widget_config['options'];
	
	//设置模板路径 
	$user_template_path = USER_TEMPLATE_PATH . 'data/widget_template/';
	
	$template_file_name = strtolower($widget_name) . '_' . strtolower($widget_template) . '.' . TEMPLATE_TYPE;
	if(is_file($user_template_path . $template_file_name)){
		//如果用户模板存在
		$smarty->template_dir = $user_template_path;
	}else{
		$template_file_name = $widget_template . '.' . TEMPLATE_TYPE;
		//使用系统的模板
		$smarty->template_dir = ROOT_PATH . '/protected/includes/widgets/' . $widget_type . '/' . $widget_name . '/template';
	}
	$smarty->assign($widget_data);
	$html = $smarty->fetch($template_file_name);
	$smarty->free();//释放
	return $html;
}
